package de.gameduell.framework.gui.animation {
	import fl.transitions.TweenEvent;

	import trace;
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.gui.animation.Tweenable;

	import com.greensock.TweenLite;

	import flash.events.EventDispatcher;

	/**
	 * @author cbra
	 */
	public class GDFastTween extends EventDispatcher implements Tweenable {
		private var parameters:Array;
		private var tween:TweenLite;
		private var tweenedObject:Object;
		private var property:String;
		private var easeType:Function;
		private var startValue:Number;
		private var endValue:Number;
		private var duration:Number;
		private var useSeconds:Boolean;
		private var targetObject:Object;
		private var playing:Boolean;

		public function GDFastTween(aTweenedObject:Object,aProperty:String,aEaseType:Function, aStartValue:Number, aEndValue:Number, aDuration:Number, aUseSeconds:Boolean) {
			parameters = new Array();
			tweenedObject = aTweenedObject;
			property = aProperty;
			easeType = aEaseType;
			startValue = aStartValue;
			endValue = aEndValue;
			duration = aDuration; // transform to seconds
			useSeconds = aUseSeconds;	
			targetObject = new Object();	
			targetObject[property] = endValue;
			targetObject["onComplete"] = onComplete;
//			targetObject["immediateRender"] = true;
			targetObject["ease"] = easeType;
			playing = false;
		}

		public function start():void {
			if(tween == null) {
				tweenedObject[property] = startValue;
				tween = new TweenLite(tweenedObject,duration,targetObject);			
			} else {
				tween.play();
			}
			playing = true;
		}
		
		public function onComplete():void{
			Debug.trace("GDFastTween::onComplete");
			playing = false; 
			dispatchEvent(new TweenEvent(TweenEvent.MOTION_FINISH,0,0));
		}
		
		public function set finish(endValue:Number):void {
			targetObject[property] = endValue;
		}
		
		public function get obj():Object{
			return parameters[0];
		} 
		  
		
		public function stop():void {
			tween.kill();
		}
		
		public function get isPlaying():Boolean {	
			return playing;
		}
	}
}
